package com.mp3s.plugin_quote.full.utils;


import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;

import com.mp3s.interface3.IDataSave;
import com.mp3s.interface3.ISchedule;
import com.mp3s.interface3.QuoteData;
import com.mp3s.interface3.TradeTime;
import com.mp3s.plugin_quote.Download;

import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;

public class schedule implements ISchedule{

    private List<String> gTaskId;           //stock id
    private String gCron = "*/30 * * * * ?";     //10秒
    private ExecutorService executor;
    private IDataSave isave;
    private int setCorePoolSize = 4;
    private int maxStockSize = 10000;

    public void CronTask(List<String> taskIdList, String cronExpression,IDataSave isave) {
        String taskId = "full-task-id";
        CronUtil.schedule(taskId, cronExpression, new Task() {
            @Override
            public void execute() {
                // 执行任务逻辑
                // System.out.println(""+DateTime.now()+","+taskId);
                //在合规时间内，进行下载
                if(false == TradeTime.isTradeTime()){
                    return;
                }
                //全市场更新数据

                String out = Download.down(taskId);
                List<QuoteData> q = utils.parseSinaQuote(out);
                isave.save(q);
            }
        });
    }

    @Override
    public void init(String taskId,String cron,IDataSave isave) {

    }

    
    @Override
    public void init(List<String> taskIdList, String cron, IDataSave ids) {

        isave = ids;
        gTaskId = taskIdList;
        // 创建一个自定义线程池
        executor = ExecutorBuilder.create()
                .setCorePoolSize(setCorePoolSize) // 设置核心线程池大小
                .setMaxPoolSize(setCorePoolSize*2) // 设置最大线程池大小
                .setWorkQueue(new LinkedBlockingQueue<>(maxStockSize)) // 设置工作队列,根据stock数量来定
                .build();

    }

    @Override
    public void start() {
        
        for(int t=0;t<setCorePoolSize;t++){

            int finalI = t;

            executor.execute(()->{

                while(true){                    

                    if(false == TradeTime.isTradeTime()){

                        try {
                            Thread.sleep(1000*60);                            
                        } catch (Exception e) {
                            // TODO: handle exception
                        }

                        continue;
                    }

                    for(int i=0;i<gTaskId.size();i++){

                        if((i%setCorePoolSize)!=finalI){
                            continue;
                        }

                        String item = gTaskId.get(i);
                        String out = Download.down(item);
                        // System.out.println("[info]download:"+item);
                        List<QuoteData> q = utils.parseSinaQuote(out);
                        isave.save(q);
    
                        try {
                            Thread.sleep(100);                            
                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                    }
    
                }


            });
        }

    }

    @Override
    public void stop() {
        executor.shutdownNow();
    }

}
